package com.yst.metho.web.application.project;

import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.CheckBox;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextField;

import com.yst.metho.web.application.team.TeamListModel;
import com.yst.metho.web.application.team.renderer.TeamListRenderer;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.ProjectConstante;
import com.yst.suivi.data.team.Team;
import com.yst.suivi.status.StatusUtil;

public class ProjectPane extends Column
{
	private TextField mCode;

	private TextField mNom;

	private CheckBox mRafFlag;

	private CheckBox mForfaitFlag;

	private SelectField mTeam;

	private String mId = null;

	public ProjectPane(Project prj, Collection teams) throws NamingException, FinderException, CreateException
	{
		super();

		setCellSpacing(new Extent(10));
		setInsets(new Insets(10));
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		add(headerGrid);
		Label label;
		label = new Label("Code");
		headerGrid.add(label);
		mCode = new TextField();
		mCode.setStyleName("TextField");
		headerGrid.add(mCode);
		label = new Label("Nom");
		headerGrid.add(label);
		mNom = new TextField();
		mNom.setStyleName("TextField");
		headerGrid.add(mNom);
		label = new Label("Saisir RAF");
		headerGrid.add(label);
		mRafFlag = new CheckBox();
		headerGrid.add(mRafFlag);
		mRafFlag.setSelected(true);
		label = new Label("Forfait");
		headerGrid.add(label);
		mForfaitFlag = new CheckBox();
		headerGrid.add(mForfaitFlag);
		mForfaitFlag.setSelected(true);
		label = new Label("Equipe");
		headerGrid.add(label);
		mTeam = new SelectField();
		TeamListModel model = new TeamListModel(teams);
		mTeam.setCellRenderer(new TeamListRenderer());
		mTeam.setModel(model);
		headerGrid.add(mTeam);

		if (null != prj)
		{
			mId = prj.getId();
			mCode.setText(prj.getCode());
			mNom.setText(prj.getNom());
			mRafFlag.setSelected(0 == prj.getRafFlag());
			label = new Label("Etat");
			headerGrid.add(label);
			label = new Label(StatusUtil.getStatusName(prj.getStatus()));
			mForfaitFlag.setSelected(prj.getTypeProj() == ProjectConstante.MODE_FORFAIT);
			headerGrid.add(label);
			if (null != prj.getTeam())
			{
				mTeam.setSelectedItem(prj.getTeam());
			}
		}
	}

	public ProjectPane(Collection teams) throws NamingException, FinderException, CreateException
	{
		this(null, teams);
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return mId;
	}

	/**
	 * @return the jour
	 */
	public String getCode()
	{
		return mCode.getText();
	}

	/**
	 * @return the mois
	 */
	public String getNom()
	{
		return mNom.getText();
	}

	/**
	 * @return the rafFlag
	 */
	public int getRafFlag()
	{
		int flag = 0;
		if (!mRafFlag.isSelected())
		{
			flag = 1;
		}
		return flag;
	}

	/**
	 * @return the rafFlag
	 */
	public int getTypeFlag()
	{
		int flag = ProjectConstante.MODE_AT;
		if (mForfaitFlag.isSelected())
		{
			flag = ProjectConstante.MODE_FORFAIT;
		}
		return flag;
	}

	/**
	 * @return the team
	 */
	public Team getTeam()
	{
		return (Team) mTeam.getSelectedItem();
	}
}
